<template>
    <div>
        <el-card style="margin-bottom:10px;">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="角色名称:">
                    <el-input v-model="formInline.name" placeholder="请输入角色名称" clearable />
                </el-form-item>
                <el-form-item label="角色标识">
                    <el-input v-model="formInline.identity" placeholder="请选择角色标识" clearable />
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="formInline.status" placeholder="请选择状态" clearable>
                        <el-option label="开" :value="true" />
                        <el-option label="关" :value="false" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onQuery"><el-icon>
                            <Search />
                        </el-icon>搜索</el-button>
                    <el-button @click="onReset"><el-icon>
                            <Refresh />
                        </el-icon>重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card>
            <div class="title">
                <h4>角色列表</h4>
                <div>
                    <el-button type="primary" @click="onAdd"><el-icon>
                            <CirclePlus />
                        </el-icon>新增角色</el-button>
                    <el-icon style="margin: 0 20px;">
                        <Clock />
                    </el-icon>
                    <el-icon style="margin-right:20px;">
                        <Clock />
                    </el-icon>
                    <el-icon style="margin-right:20px;">
                        <Clock />
                    </el-icon>
                </div>
            </div>
            <el-table :data="tableData" height="460" style="width: 100%;margin-bottom:5px;" border>
                <el-table-column fixed prop="id" label="角色编号" width="180" />
                <el-table-column prop="name" label="角色名称" width="100" />
                <el-table-column prop="identity" label="角色标识" width="120" />
                <el-table-column prop="status" label="状态" width="120" />
                <el-table-column prop="remark" label="备注" show-overflow-tooltip />
                <el-table-column prop="createtime" label="创建时间" width="180" />
                <el-table-column fixed="right" label="操作" width="120">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="onEdit(scope.row)">
                            修改
                        </el-button>
                        <!-- <el-button link type="primary" size="small">删除</el-button> -->
                        <el-popconfirm title="是否需要删除?" @confirm="onConfirm(scope.row)">
                            <template #reference>
                                <el-button link type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="->,total,prev, pager, next" :total="total"
                @current-change="haddleChange" />
        </el-card>
        <el-dialog v-model="dialogFormVisible" :title="flag ? '新增角色' : '编辑角色'" width="500">
            <el-form :model="form" label-width="80px" :rules="rules">
                <el-form-item label="角色名称" prop="name">
                    <el-input v-model="form.name" autocomplete="off" placeholder="请输入角色名称" />
                </el-form-item>
                <el-form-item label="角色标识" prop="identity">
                    <el-input v-model="form.identity" autocomplete="off" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark" type="textarea" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="onSure">
                        确认
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script>
import request from '@/utils/request.js'
import { ElMessage } from 'element-plus'
export default {
    data() {
        return {
            total: 0,
            formInline: {
                name: '',
                identity: '',
                status: '',
                pagination: 1,
                pageNum: 10
            },
            tableData: [],
            dialogFormVisible: false,
            form: {
                name: '',
                identity: '',
                remark: ''
            },
            flag: true,
            editId: '',
            rules: {
                name: [
                    { required: true, message: '请输入角色名称', trigger: 'blur' },
                ],
                identity: [
                    { required: true, message: '请输入角色标识', trigger: 'blur' },
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            request.get('/list', { params: this.formInline }).then(res => {
                this.tableData = res.data.data
                this.total = res.data.total
            })
        },
        onQuery() {
            this.getList()
        },
        onReset() {
            this.formInline = {
                name: '',
                identity: '',
                status: '',
                pagination: 1,
                pageNum: 10
            }
            this.getList()
        },
        haddleChange(val) {
            this.formInline.pagination = val
            this.getList()
        },
        onAdd() {
            this.dialogFormVisible = true
            this.flag = true
            this.form = {
                name: '',
                identity: '',
                remark: ''
            }
        },
        onSure() {
            // 请求接口 -- 新增的时候调/add ,修改的时候调/edit
            this.flag ?
                request.post('/add', this.form).then(res => {
                    console.log(res)
                    if (res.data.code == 200) {
                        ElMessage({
                            message: '新增成功',
                            type: 'success',
                        })
                        this.dialogFormVisible = false
                        this.getList()
                    }
                })
                :
                request.post('/edit', { ...this.form, id: this.editId }).then(res => {
                    if (res.data.code == 200) {
                        ElMessage({
                            message: '修改成功',
                            type: 'success',
                        })
                        this.dialogFormVisible = false
                        this.getList()
                    }
                })
        },
        onEdit(row) {
            this.dialogFormVisible = true
            this.flag = false
            console.log(row)
            this.form.name = row.name
            this.form.identity = row.identity
            this.form.remark = row.remark
            // 存id
            this.editId = row.id

        },
        onConfirm(row) {
            console.log(row.id)
            request.post('/delete', { id: row.id }).then(res => {
                if (res.data.code == 200) {
                    ElMessage({
                        message: '删除成功',
                        type: 'success',
                    })
                    this.getList()
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 220px;
}

.title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}
</style>